<?php namespace Common\Model;
use Hdphp\Model\Model;

class sc_goods_attr extends Model{
	   
	//指定表
	 protected $table="sc_goods_attr";
	 
	 //引用类型属性表
	 private $TypeAttrModel;
	 public function __init(){
	 	$this->TypeAttrModel=M('\Common\Model\sc_typeAttr');
	 }

	//商品属性添加
	public function addGoodsAttr($gid){
        //添加属性
		foreach(Q('post.attr') as $taid=>$v){
			
			if($v){
				$data=array(
				    'gtvalue'=>$v,
				    'sc_type_attr_taid'=>$taid,
				    'sc_goods_gid'=>$gid
				);
			$ecp=$this->add($data);
			}	
		}
		
		//添加规格
		$spec=Q('post.spec');
		if($spec){
			$value=$spec['value'];
			foreach($value as $taid=>$v){
				foreach($v as $kk=>$vv){
					if($vv){
						$data = array(
					    'gtvalue'=>$vv,
					    'sc_type_attr_taid'=>$taid,
					    'sc_goods_gid'=>$gid,
					    'added'=>$spec['added'][$taid][$kk]
					);
					$ecp=$this->add($data);
					}
				}
			}
		}
		if(!$ecp) return false;
		return true;
	}
	
	
	//商品属性编辑
	public function saveOneGoodsAttr(){
		$gid=Q('post.gid');
		//删除旧的商品属性
		$this->where('sc_goods_gid',$gid)->delete();
		//修改商品属性
		foreach(Q('post.attr') as $taid=>$v){
			if($v){
				$data=array(
				'gtvalue'=>$v,
				'sc_type_attr_taid'=>$taid,
				'sc_goods_gid'=>$gid
			   );
			   $ecp=$this->add($data); 
			}
		}
		
		//修改商品规格
		$spec=Q('post.spec');
		if($spec){
			$value=$spec['value'];
			foreach($value as $taid=>$v){
				foreach($v as $kk=>$vv){
					if($vv){
						$data = array(
					    'gtvalue'=>$vv,
					    'sc_type_attr_taid'=>$taid,
					    'sc_goods_gid'=>$gid,
					    'added'=>$spec['added'][$taid][$kk]
					);
					$ecp=$this->add($data);
					}
				}
			}
		}
		if(!$ecp) return false;
		return true;
	}
	
	
	
	public function getOneGoodsAttr(){
		$gid=(Q('get.gid'));
		$data=$this->join('sc_type_attr','sc_type_attr_taid','=','taid')
		->where('sc_goods_gid',$gid)
		->orderBy('gtid','DESC')
		->get();
		//获得taid就是为了以后排除异己选过的类型属性
	    $taid=[];
		foreach($data as $k=>$v){
			$data[$k]['tavalue']=explode("|", $v['tavalue']);
			$taid[]=$v['taid'];	
			$tid=$data[$k]['sc_type_tid'];
		}

		$taidStr=implode(",", $taid);
		$NotInTypeAttrData=$this->TypeAttrModel->getNotInTypeAttr($taidStr,$tid);
		foreach($NotInTypeAttrData as $v){
			$data[]=array(
			     'taid'=>$v['taid'],
			     'gtvalue'=>'',
			     'taname'=>$v['taname'],
			     'tavalue'=>explode('|', $v['tavalue']),
			     'class'=>$v['class']
			);
		}
		return $data;

	}
	
	//货品表使用*********************************************
	//通过传递的商品gid获取商品属性和类型属性信息
	public function getGoodsListAttr(){
		//获取当前gid
		$gid=Q('get.gid');
		//通过gid，商品属性表和类型属性表连接查询
		$data = $this->join('sc_type_attr','sc_type_attr_taid','=','taid')
		       ->where("sc_goods_gid={$gid} AND class=1")
			   ->groupBy('taid')
			   ->orderBy('gtid','ASC')
			   ->get();
			   
	    //添加货品的时候下拉菜单所选择的内容（一定是商品本身属性）
	    foreach($data as $k=>$v){
	    	$data[$k]['value']=$this->where('sc_type_attr_taid',$v['taid'])
	    	                        ->where('sc_goods_gid',$gid)
									->orderBy('gtid','ASC')
	    	                        ->get();
	    }
		return $data;
			   
	}
	
	
	
	
	
	
	//前台调用····························································
	//获取当前当前分类下所有goods的属性
	public function getGoodsAttr($gids){
		//将当前分类下所有goods的gids数组转化成字符串
		$gidsStr=implode(",", $gids);

		return $this->where("sc_goods_gid IN ($gidsStr)")->groupBy('gtvalue')->get();
		
	}
	
	
	
	
	
}

?>
